#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const ll N = 2e6 + 10, INF = 0x3f3f3f3f;

ll n;
string s;
ll dp[N][10], f[10];

void solve()
{
    memset(dp, 0, sizeof dp);
    memset(f, 0, sizeof f);
    cin >> s;n = s.length();s = " " + s;
    dp[0][0] = 1;
    for (ll i = 1;i <= n;i++) {
        dp[i][0] = 1;
        ll x = s[i] - '0';
        for (ll j = 0;j <= 8;j++)
            dp[i][(x + j) % 9] += dp[i - 1][j];
    }

    for (int j = 0;j < 9;j++)
        for (int i = 1;i <= n;i++) f[j] += dp[i][j];

    ll cnt = n;
    for (ll l = 1;l <= n;l++) {
        if (s[l] != '0') continue;
        ll r = l;
        while (r + 1 <= n && s[r + 1] == '0') r++;
        ll len = r - l + 1;
        cnt += len * (len + 1) / 2;
        l = r;
    }

    f[0] -= cnt;

    ll ans = 0;
    for (ll i = 0;i <= 8;i++)
        if (i == 0) ans += f[i] * 9;
        else ans += f[i] * i;

    cout << ans << '\n';
}

signed main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    ll T = 1;
    cin >> T;
    for (ll i = 1;i <= T;i++) {
        solve();
    }
}